 package com.baiytfp.hf.HFDataExchange.common.utils.auth;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.baiytfp.hf.HFDataExchange.common.model.base.TableName;
import com.baiytfp.hf.HFDataExchange.common.model.state.Status;
import com.baiytfp.hf.HFDataExchange.common.model.state.TreeType;
import com.baiytfp.hf.HFDataExchange.common.utils.CommonUtil;
import com.baiytfp.hf.baiytfp.framework.util.CacheUtil;
import com.baiytfp.hf.baiytfp.hfutils.HFDBHelper;


public class AuthUtils {
	
	private static final String KEY_SUPER_ADMIN = "$super_admin_users_array";
	
	/**
	 * 用户 ：1
	 */
	public static final int AUTH_OBJ_TYPE_USER = 1;
	/**
	 * 岗位 ：2
	 */
	public static final int AUTH_OBJ_TYPE_POSI = 2;
	/**
	 * 角色 ：3
	 */
	public static final int AUTH_OBJ_TYPE_ROLE = 3;
	
	/**
	 * 用户是否有权限访问指定系统链接
	 * @param userCode
	 * @param url
	 * @return
	 */
	public static boolean canDoMenu(String userCode,String url){
		if( null == url) return false;
		List<Map> menus = getMenu(userCode);
		if(  null != menus && menus.size()>0) {
			Iterator iterator = menus.iterator();
			Map menu = null;
			while(iterator.hasNext()) {
				menu = (Map) iterator.next();
				if( null != menu.get("url") && ((String)menu.get("url")).contains(url) ) {
					return true;
				}
			}
		}
		return false;
	}
	
	
	/*public static boolean canDoFunc(String userCode,String... funcCode) {
		
		if(null == funcCode || funcCode.length < 1) return false;
		int count = 0;
		List<Map> funcs = getFunc(userCode);
		if(  null != funcs && funcs.size()>0) {
			Iterator iterator = funcs.iterator();
			List<String> list = Arrays.asList(funcCode);
			Map func = null;
			while(iterator.hasNext()) {
				func = (Map) iterator.next();
				if(list.contains(func.get("func_code"))) {
					count++;
				}
			}
			return count == list.size();
		}
		
		return false;
	}*/
	
	
	
	
	/**
	 * 判定用户是否为超级管理员
	 * @param userCode
	 * @return
	 */
	public static boolean isAdmin(String userCode) {
		boolean rs = false;
		try {
			Object[] usersArray = (Object[]) CacheUtil.getCache().get(KEY_SUPER_ADMIN);
			if(CommonUtil.isEmpty(usersArray)) {
				List<String> users = HFDBHelper.querySql( "select user_code from "+TableName.USER_ACCOUNT+" where  is_admin = ? ", 
						String.class, new Object[] {Status.YES.val()});
				if(null != users ) {
					CacheUtil.getCache().set(KEY_SUPER_ADMIN, users.toArray(), 3600);
					rs =   users.contains(userCode);
				}
			}else {
				rs = Arrays.asList(usersArray).contains(userCode);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return rs;
	}
	
	

	public static List<Map> getMenu(String userCode) {
		StringBuffer sql = new StringBuffer();
		sql.append("select v1.*,tree.* \n").append("  from " + TableName.MENU + " v1 \n")
				.append("  join (select distinct t.node_seq, t.tree_type \n")
				.append("          from " + TableName.AUTH + " t \n")
				.append("         where (t.is_disabled = ? or t.is_disabled is null) \n")
				.append("           and t.is_auth = ? \n").append("           and t.tree_type = ? \n")
				.append("           and ((t.obj_type = ? and t.obj_code = ?) or \n")
				.append("               (t.obj_type = ? and \n").append("               t.obj_code in \n")
				.append("               (select c.role_code \n")
				.append("                    from " + TableName.USER_ROLE + " c  join "+TableName.ROLE+" z on c.role_code = z.role_code  \n")
				.append("                   where c.user_code = ? )))) v2 \n")
				.append("    on v1.tree_id = v2.node_seq   ")
				.append("    left join ").append(TableName.TREE).append(" tree on v1.tree_id = tree.id ")
				.append("  order by tree.node_deep asc, tree.parent_tree_code, tree.node_order asc");

		Object[] params = new Object[] { Status.NO.val(), Status.YES.val(), TreeType.MANAGE_MENU.getKey(),
				AUTH_OBJ_TYPE_USER, userCode, AUTH_OBJ_TYPE_ROLE, userCode };
		List<Map> rs= doApi(sql.toString(), params);
		return rs;
	}

	/*public static List<Map> getFunc(String userCode) {
		StringBuffer sql = new StringBuffer();
		sql.append("select v1.* \n").append("  from " + TableName.FUNC + " v1 \n")
				.append("  join (select distinct t.func_id, t.func_type \n")
				.append("          from " + TableName.AUTH + " t \n")
				.append("         where (t.is_disabled = ? or t.is_disabled is null) \n")
				.append("           and t.is_auth = ? \n").append("           and t.func_type = ? \n")
				.append("           and ((t.obj_type = ? and t.obj_code = ?) or \n")
				.append("               (t.obj_type = ? and \n").append("               t.obj_code in \n")
				.append("               (select c.role_code \n")
				.append("                    from " + TableName.USER_ROLE + " c join "+TableName.ROLE+" z on c.role_code = z.role_code \n")
				.append("                   where c.user_code = ? )))) v2 \n")
				.append("    on v1.tree_id = v2.func_id ");

		Object[] params = new Object[] { Status.NO.val(), Status.YES.val(), Status.TYPE_FUNC.val(),
				Status.OBJ_TYPE_USER.val(), userCode, Status.OBJ_TYPE_ROLE.val(), userCode };

		return doApi(sql.toString(), params);
	}*/

	

	private static List<Map> doApi(String sql, Object... params) {
		List<Map> rs = null;
		try {
			rs = HFDBHelper.querySql(sql, Map.class, params);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return rs;
	}
	
	
}
 